package com.example.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class AcrossConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")// 针对所有路径应用跨域配置
                .allowedOrigins("*")// 允许所有来源
                .allowedHeaders("*")// 允许所有请求头
                .allowedMethods("*");// 允许所有请求方法

//                .allowedOrigins("http://example.com", "http://another-example.com") // 明确列出允许的源
//                .allowedMethods("GET", "POST", "PUT", "DELETE") // 允许的HTTP方法
//                .allowCredentials(true) // 允许携带凭证
//                .maxAge(3600); // 预检请求的有效期（秒）
    }


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String osName = System.getProperty("os.name");
        String fileUploadResources = "/**";

        String win ="win";
        if (osName.toLowerCase().startsWith(win)){
            registry.addResourceHandler(fileUploadResources)
                    .addResourceLocations("file:C:\\upload\\");

            registry.addResourceHandler(fileUploadResources)
                    .addResourceLocations("file:/upload/");
        }
    }

}
